using System;
using System.Collections.Generic;
using Sce.PlayStation.Core.Imaging;

namespace Nyanimals
{
    public sealed class CSimpleFont
    {
        int charWidth;
        int charHeight;
        int numRow;
        int numCol;
        
        bool initialized;
        
        ImageRect letterRect; // each letters size
        Image fontImg; // font image
        Dictionary<char, Image> LetterImages; // cached cropped images
        
        public int CharWidth
        {
            get { return charWidth;}
        }
        public int CharHeight
        {
            get { return charHeight;}
        }
        public int NumRow
        {
            get { return numRow;}
        }
        public int NumCol
        {
            get { return numCol;}
        }
        
        private static readonly CSimpleFont instance = new CSimpleFont();
        public static CSimpleFont Instance
        {
            get { return instance;}
        }
        
        private CSimpleFont()
        {
            // for the imaging and text stuff
            letterRect = new ImageRect(0, 0, 16, 16);
            fontImg = new Image("/Application/Resources/Fonts/font.jpg");
            LetterImages = new Dictionary<char, Image>();
            
            // the specific numbers
            charWidth = 16;
            charHeight = 16;
            numRow = fontImg.Size.Width / charWidth;
            numCol = fontImg.Size.Height / charHeight;
            
            initialized = false;
        }
        
        public void Init()
        {
            if(initialized)
            {
                return;
            }
            // precache all the letters
            int letterCount = numRow * numCol;
            for(int letter = 0; letter < letterCount; ++letter)
            {
                CropLetter((char)letter);
            }
            initialized = true;
        }
        public void Shutdown()
        {
            foreach(KeyValuePair<char,Image> img in LetterImages)
            {
                img.Value.Dispose();
            }
            LetterImages.Clear();
            initialized = false;
        }
        public Image GetLetter(char l)
        {
            // make sure we already cached the letter
            bool hasChar = LetterImages.ContainsKey(l);
            if(hasChar == false)
            {
                CropLetter(l);
            }
            
            // return the letter
            return LetterImages[l];
        }
        
        private void CropLetter(char l)
        {
            // get the letter to crop out
            int charIndex = (int)l;
            int X = charIndex % numCol;
            int Y = charIndex / numRow;
            letterRect.X = X * charWidth;
            letterRect.Y = Y * charHeight;
            
            // put the letter onto the cache
            LetterImages[l] = fontImg.Crop(letterRect);
        }
    }
}

